package com.sso.controller;

import com.sso.service.UserService;
import com.sso.utils.Constants;
import com.sso.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author : alex
 * @date : 2020/1/28
 */
@Controller
@RequestMapping(value = "/web")
public class SsoWebController {

    @Autowired
    UserService userService;

    /**
     * 跳转登录页
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/login")
    public ModelAndView login(HttpServletRequest request, HttpServletResponse response, ModelAndView modelAndView) {
        String token = userService.getCookies(Constants.TOKEN_COOKIE_KEY, request);
        String service = request.getParameter("service");
        if (userService.verifyToken(token) && !StringUtils.isEmpty(service)) {
            service = service + "?token=" + token;
            modelAndView.setViewName("redirect:" + service);
        } else {
            userService.deleteCookieToken(token, response);
            modelAndView.addObject("service", service);
            modelAndView.setViewName("login");
        }
        return modelAndView;
    }


    /**
     * 用户登录
     *
     * @param request
     * @param response
     * @param modelAndView
     * @return
     */
    @RequestMapping(value = "/signIn")
    public ModelAndView signIn(HttpServletRequest request, HttpServletResponse response, ModelAndView modelAndView) {
        String service = request.getParameter("service");
        String userName = request.getParameter("userName");
        String password = request.getParameter("password");
        //1.1、验证登录信息合法
        if (userService.verifyUser(userName, password)) {
            //2、创建token
            String token = TokenUtil.createToken(userName);
            //3、将token保存到cookie和redis中
            userService.saveToken(token, response);
            if (StringUtils.isEmpty(service)) {
                //4.1、无returnUrl，跳转默认页面
                modelAndView.setViewName("index");
            } else {
                //4.2、转发到业务系统
                service = service + "?token=" + token;
                modelAndView.setViewName("redirect:" + service);
            }
        } else {
            //1.2、验证失败，跳转登录页面
            modelAndView.setViewName("login");
            modelAndView.addObject("service", service);
        }
        return modelAndView;
    }

    /**
     * 退出登录
     * @param request
     * @param response
     * @param modelAndView
     * @return
     */
    @RequestMapping(value = "/logout")
    public ModelAndView logout(HttpServletRequest request, HttpServletResponse response, ModelAndView modelAndView) {
        //1、验证cookie中token
        String service = request.getParameter("service");
        String token = userService.getCookies(Constants.TOKEN_COOKIE_KEY, request);
        if (userService.verifyToken(token)) {
            //2、删除cookie和redis中token
            userService.deleteToken(token, response);
        }
        //3、跳转登录页面
        modelAndView.addObject("service", service);
        modelAndView.setViewName("login");
        return modelAndView;
    }
}
